package com.reddys.rad.inventory.controller;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.reddys.rad.inventory.db.Vendor;
import com.reddys.rad.inventory.utils.EntityManagerImpl;
import com.reddys.rad.inventory.utils.Utilities;
import java.math.BigInteger;


import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.util.Callback;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;


/**
 * FXML Controller class
 *
 * @author Arun
 */
public class VendormasterController implements Initializable {

    @FXML
    private TextField registorID;
    @FXML
    private TextField registorName;
    @FXML
    private ComboBox registrationType;
    
    @FXML
    private TextField contactPerson;
    @FXML
    private TextField contactMobile;
    @FXML
    private TextField addressLine1;
    @FXML
    private TextField addressLine2;
    @FXML
    private TextField city;
    @FXML
    private TextField state;
    @FXML
    private TextField country;
    @FXML
    private TextField pincode;
    @FXML
    private TextField bankName;
    @FXML
    private TextField branch;
    @FXML
    private TextField accountHolder;
    @FXML
    private TextField accountNumber;
    @FXML
    private TextField ifscCode;
    @FXML
    private TextField accountType;
    @FXML
    private TextField panNumber;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
     ObservableList list=FXCollections.observableArrayList();
     list.add("Customer");
     list.add("Vendor");
     list.add("Seller");
     list.add("Retailer");
     registrationType.setItems(list);
    }

    @FXML
    public void save(ActionEvent event) {
       if(validateSave()){ 
           try{
        Vendor vendor = new Vendor();
        vendor.setVendorname(registorName.getText());
        vendor.setVendortype(registrationType.getSelectionModel().getSelectedItem().toString());
        vendor.setContactperson(contactPerson.getText());
        vendor.setContactpersontelephone(contactMobile.getText());
        //address
        vendor.setContactpersonaddress1(addressLine1.getText());
        vendor.setContactpersonaddress2(addressLine2.getText());
        vendor.setContactpersoncity(city.getText());
        vendor.setContactpersonstate(state.getText());
        vendor.setContactpersoncountry(country.getText());
        vendor.setContactpersonpincode(pincode.getText());
        
        //Bank details
        vendor.setBank(bankName.getText());
        vendor.setBranch(branch.getText());
        vendor.setAccountholdername(accountHolder.getText());
        vendor.setAccountnumber(accountNumber.getText());
        vendor.setAcoounttype(accountType.getText());
        vendor.setIfsccode(ifscCode.getText());
        vendor.setPannumber(panNumber.getText());
        
        
        
//        product.setProductId(productID.getText());
//        product.setProductDescription(productName.getText());
//        product.setItemCode(itemCode.getText());
//        product.setBatchCode(batchCode.getText());
//        product.setStandardUom(uom.getText());
//        product.setReorderLevel(new BigInteger(reorderValue.getText()));
        
       /*EntityManager manager=getDBConnetion();
        manager.getTransaction().begin();
        manager.persist(product);
        manager.getTransaction().commit();*/
        vendor=(Vendor) EntityManagerImpl.persist(vendor);
         registorID.setText(vendor.getVendorid());
         Utilities.createDialog("Transaction Completed Successfully");
        }catch(Exception e){
            e.printStackTrace();
            Utilities.createDialog("Transaction Failure");
        }
       }
    }

    public static EntityManager getDBConnetion() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("purchasemodule");
        EntityManager manager = factory.createEntityManager();
        return manager;

    }
    
//    public  void populateProducttable(){
//        EntityManager manager=EntityManagerImpl.getEntityManager();
//        Query query=manager.createNamedQuery("Product.findAll");
//          List<Product> list= (List<Product>) query.getResultList();
//           
//           ObservableList list2= FXCollections.observableArrayList();
//         for(Product l:list){
//             ObservableList list1= FXCollections.observableArrayList();
//             list1.add(l.getProductId());
//             list1.add(l.getProductDescription());
//             list1.add(l.getItemCode());
//             list1.add(l.getBatchCode());
//             list1.add(l.getStandardUom());
//             list1.add(l.getReorderLevel().toString());
//             System.out.println("----------"+l.getReorderLevel());
//            list2.add(list1);
//         } 
//         
//       
//         productTable.getItems().clear();;
//        productTable.setItems(list2);
//        System.out.println("heelooooooooooo"+list2.size());
//        tableProductID.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){
//
//            @Override
//            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
//              return new SimpleStringProperty( 
//                      cdf.getValue().get(0).toString());
//            }
//           
//
//           
//        });
//        
//        tableProductName.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){
//
//            @Override
//            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
//              return new SimpleStringProperty( cdf.getValue().get(1).toString());
//            }
//           
//
//           
//        });
//        
//        tableItemCode.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){
//
//            @Override
//            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
//              return new SimpleStringProperty( cdf.getValue().get(2).toString());
//            }
//           
//
//           
//        });
//        
//        tableBatchCode.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){
//
//            @Override
//            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
//              return new SimpleStringProperty( cdf.getValue().get(3).toString());
//            }
//            
//           
//
//           
//        });
//        
//        tableProductUOM.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){
//
//            @Override
//            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
//              return new SimpleStringProperty( cdf.getValue().get(4).toString());
//            }
//           
//
//           
//        });
//        
//        tableReorderValue.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){
//
//            @Override
//            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
//              return new SimpleStringProperty( cdf.getValue().get(5).toString());
//            }
//           
//
//           
//        });
//        //productTable.getItems().add(uom);
//       
//        
//    }
//    
//    
    
    public boolean validateSave(){
        String type = (String)registrationType.getSelectionModel().getSelectedItem();
      if(Utilities.isNull(type)){
          Utilities.createDialog("Please select registration type");
          return false;
      }

      if(Utilities.isNull(registorName.getText())){
          Utilities.createDialog("Please enter name");
          return false;
      }
      if(Utilities.isNull(contactMobile.getText())){
          Utilities.createDialog("Please enter mobile number");
          return false;
      }
      if(Utilities.isNull(contactPerson.getText())){
          Utilities.createDialog("Please enter contact person");
          return false;
      }
      if(Utilities.isNull(addressLine1.getText())){
          Utilities.createDialog("Please enter address line 1");
          return false;
      }
      if(Utilities.isNull(city.getText())){
          Utilities.createDialog("Please enter city");
          return false;
      }
      if(Utilities.isNull(state.getText())){
          Utilities.createDialog("Please enter state");
          return false;
      }
      if(Utilities.isNull(country.getText())){
          Utilities.createDialog("Please enter country");
          return false;
      }
      if(Utilities.isNull(pincode.getText())){
          Utilities.createDialog("Please enter pincode");
          return false;
      }
      if(Utilities.isNull(bankName.getText())){
          Utilities.createDialog("Please enter bank name");
          return false;
      }
      if(Utilities.isNull(accountNumber.getText())){
          Utilities.createDialog("Please enter account number");
          return false;
      }
      if(Utilities.isNull(accountHolder.getText())){
          Utilities.createDialog("Please enter a/c holder name");
          return false;
      }
      if(Utilities.isNull(accountType.getText())){
          Utilities.createDialog("Please enter account type");
          return false;
      }
      if(Utilities.isNull(branch.getText())){
          if(Utilities.isNull(ifscCode.getText())){
            Utilities.createDialog("Please enter either branch or IFSC code");
            return false;            
          }
      }      
        return true;
    }
    
    
}
